package com.cxh.rmsq.wxapi

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.SPUtils
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.event.LiveEventBusCode.RECHARGE_CANCEL_MESSAGE
import com.cxh.rmsq.event.LiveEventBusCode.RECHARGE_FAIL_MESSAGE
import com.cxh.rmsq.event.LiveEventBusCode.RECHARGE_SUCCESS_MESSAGE
import com.cxh.rmsq.net.newNet.TreasureViewModel
import com.cxh.rmsq.utils.ToastUtils
import com.google.gson.Gson
import com.jeremyliao.liveeventbus.LiveEventBus
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory


class WXPayEntryActivity : Activity(), IWXAPIEventHandler {
    private lateinit var iwxapi: IWXAPI
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(TreasureViewModel::class.java)
    }

    override fun onResp(resp: BaseResp) {
        Log.e("微信=", Gson().toJson(resp))
        if (resp.type == ConstantsAPI.COMMAND_PAY_BY_WX) {
            val code = resp.errCode
            when (code) {
                0 -> {
                    LiveEventBus.get(RECHARGE_SUCCESS_MESSAGE).post("")
                    //EventBus.getDefault().post(EventMessage(EventMessage.EventState.WECHAT_PAY_SUCCESS, code))
                    ToastUtils.showToast("购买成功")
                    finish()
                }

                -1 -> {
                    LiveEventBus.get(RECHARGE_FAIL_MESSAGE).post("")
                    //EventBus.getDefault().post(EventMessage(EventMessage.EventState.WECHAT_PAY_ERROR, code))
                    ToastUtils.showToast("购买失败")
                    finish()
                }

                -2 -> {
                    LiveEventBus.get(RECHARGE_CANCEL_MESSAGE).post("")
                    //EventBus.getDefault().post(EventMessage(EventMessage.EventState.WECHAT_PAY_ERROR, code))
                    ToastUtils.showToast("购买取消")
                    viewModel.deleteTreasureOrder(SPUtils.getInstance().getString("orderId")) {
                        if (it.code == 200) {
                            finish()
                        } else {
                            ToastUtils.showToast(it.msg)
                        }
                    }
                }

                else -> {
                    LiveEventBus.get(RECHARGE_FAIL_MESSAGE).post("")
                    //EventBus.getDefault().post(EventMessage(EventMessage.EventState.WECHAT_PAY_ERROR, code))
                    ToastUtils.showToast("购买失败 other")
                    setResult(RESULT_OK)
                    finish()
                }
            }
        }
    }

    override fun onReq(req: BaseReq) {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        iwxapi = WXAPIFactory.createWXAPI(this, Constant.WECHAT_APP_ID)
        iwxapi.registerApp(Constant.WECHAT_APP_ID)
        iwxapi.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        iwxapi.handleIntent(intent, this)
    }
}